import pathToRegexp from 'path-to-regexp';
import { goodsSearch, goodsFilter,getClasslistApi } from './api';
import { Toast } from 'antd-mobile';
import { parse } from 'query-string';

export default {
    namespace: 'goodsearch',
    state: {
        list:[],
        hasMore:true,
        classList: [],
        filter:{
            searchType:"",
            keyword:"",
            pageNo:1,
            brandId:"",
            areaId:"",
            specFilter:"",
            sortField:"",
            sortOrder:"asc",
            identification:"1",
            storeId:""
        },
        specList:[],
        minimumPrice:'',
        maximumPrice:''
    },
    reducers: {
        save(state, { data }) {
            return { ...state, ...data };
        },
        changeSpecCheck(state, { index, sonIndex, check }){
            let arr =  state.specList.map((v,i,a) => {
                 if(i==index){
                    let newSpecList = v.specValueList.map((vv,ii,aa) => {
                         if(sonIndex==ii){
                             return { ...vv, check: check }
                         }else {
                             return vv
                         }
                     })
                     return { ...v, specValueList:newSpecList };
                 }else {
                     return v
                 }
             })

            return { ...state, specList:arr }
        }
    },
    effects: {
        *getClasslistEFF({ }, { put, call }) {
            Toast.loading();
            const result = yield call(getClasslistApi);
            if (result.code == 200) {
                Toast.hide();
                yield put({ type: 'save', data: { classList: result.ret.list || [] } });
                // if (result.ret && result.ret.list.length > 0) {
                //   yield put({ type: 'getGoodsClassEFF', advid: result.ret.list[0].gcAdvid, pId: result.ret.list[0].gcId });
                // }
            } else {

            }
        },
        *getGoodsSearchEFF({ obj }, { put, call, select }) {
            //Toast.loading('加载中...', 3);
            const goodsearch = yield select((state)=>state.goodsearch);
            const specList = goodsearch.specList;
            const specValueIds = [];
            const specbandId = [];
            console.log(goodsearch)
            for(let i=0; i<specList.length; i++){
                const specValueIdItem = [];
                for(let j=0; j<specList[i].specValueList.length; j++){
                    const specValueListItem = specList[i].specValueList[j];
                    if(specValueListItem.check==true){
                       if(specValueListItem.isBrand==0){
                           specValueIdItem.push(specValueListItem.id)
                       }else if(specValueListItem.isBrand==1){
                           specbandId.push(specValueListItem.id)
                       }
                    }
                }
                if (specValueIdItem.length > 0) {
                    specValueIds.push(specValueIdItem.join(":"));
                }
            }

            console.log(specValueIds,specbandId);

            const result = yield call(goodsSearch, { ...goodsearch.filter, ...obj, minimumPrice:goodsearch.minimumPrice, maximumPrice:goodsearch.maximumPrice, brandId:specbandId.join(","), specFilter:specValueIds.join(",") });
            if ( result.code == 200 ) {
                Toast.hide();
                console.log(result)
                if( result.ret != '' ){
                    if( obj.pageNo !==undefined && obj.pageNo == 1 ){
                        yield put({ type: 'save', data: { ...result.ret, list:[ ...result.ret.list ], hasMore: result.ret.list.length==20, filter:{ ...goodsearch.filter, ...obj } } });
                    }else {
                        yield put({ type: 'save', data: { ...result.ret, list:[ ...goodsearch.list, ...result.ret.list ], hasMore: result.ret.list.length==20, filter:{ ...goodsearch.filter, ...obj } } });

                    }
                    //Toast.hide();
                    //console.log([ ...goodsearch.list, ...result.ret.list ]);
                }else {
                    yield put({ type: 'save', data: { hasMore: false ,list:[], filter:{ ...goodsearch.filter, ...obj }  }});
                }
                return { r:true };
            } else {
                yield put({ type: 'save', data: { hasMore: false ,list:[], filter:{ ...goodsearch.filter, ...obj }  }});
                Toast.fail(result.msg, 1.5);
            }
        },

        *getGoodsFilterEFF({ obj }, { put, call, select }) {

            const result = yield call(goodsFilter, obj);
            if ( result.code == 200 ) {
                yield put({ type: 'save', data: { specList: result.ret.specList } });
            } else {

            }
        },

    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname, search, params }) => {
                //console.log(pathname)

                if (pathname.includes('/goodsearch/')) {
                    const match = pathToRegexp('/goodsearch/:searchType').exec(pathname);
                    dispatch({ type: 'getGoodsSearchEFF',
                        obj:{
                            searchType:match[1],
                            pageNo:1,
                            keyword: parse(search).keyword === undefined ? "" : parse(search).keyword
                        }
                    });
                    dispatch({ type: 'getClasslistEFF' });
                    dispatch({ type: 'getGoodsFilterEFF',
                        obj:{
                            searchType:match[1],
                            keyword: parse(search).keyword === undefined ? "" : parse(search).keyword
                        }
                    });

               }
            });
        }
    },
};
